﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace net.xBei.Helper
{
    /// <summary>
    /// 正则表达式快捷操作
    /// 包含了一些常用表达式
    /// </summary>
    public static class RegexHelper
    {
        /// <summary>
        /// 是否匹配
        /// </summary>
        /// <param name="input"></param>
        /// <param name="pattern"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public static bool IsMatch(this string input, string pattern, bool ignoreCase = false)
        {
            var options = RegexOptions.Compiled;
            if (ignoreCase) {
                options |= RegexOptions.IgnoreCase;
            }
            return Regex.IsMatch(input, pattern, options);
        }
        /// <summary>
        /// 是否匹配
        /// </summary>
        /// <param name="regex"></param>
        /// <param name="input"></param>
        /// <param name="match"></param>
        /// <returns></returns>
        public static bool IsMatch(this Regex regex, string input, out Match match) {
            match = regex.Match(input);
            return match.Success;
        }
        /// <summary>
        /// 是否匹配
        /// </summary>
        /// <param name="regex"></param>
        /// <param name="input"></param>
        /// <param name="matchs"></param>
        /// <returns></returns>
        public static bool IsMatchs(this Regex regex, string input, out MatchCollection matchs) {
            matchs = regex.Matches(input);
            return matchs.Any();
        }
        /// <summary>
        /// 是否匹配
        /// </summary>
        /// <param name="input"></param>
        /// <param name="pattern"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public static bool IsMatch(this string input, IEnumerable<string> pattern, bool ignoreCase = false)
        {
            foreach(var str in pattern)
            {
                var ismatch = ignoreCase
                                ? input.Contains(str, StringComparison.OrdinalIgnoreCase)
                                : input.Contains(str);
                if (ismatch)
                    return true;
            }
            return false;
        }
    }
}
